#include "CLIENT.h"

BEGIN_EVENT_TABLE(CLIENT, wxFrame)
	EVT_TIMER	(TIMER_ID, CLIENT::OnTimer)
END_EVENT_TABLE()

DECLARE_APP(ClientApp)

IMPLEMENT_APP(ClientApp)

/**
 *	This creates a CLIENT on a frame, but hides the frame as the only one we want to be visible is the GUI frame
 */
bool ClientApp::OnInit()
{
	// Creates a new hidden frame and creates a new 'CLIENT'
	CLIENT * frame = new CLIENT();
	frame->Show(false);
	return(true);
}

/**
 *	This is the constructor for the class 'CLIENT'
 *	It initialises an attempt to connect to a server and starts the timer
 */
CLIENT::CLIENT() : input_timer(this, TIMER_ID), wxFrame(NULL, wxID_ANY, wxT(""))
{	
	// Connects to the server and then begins to look for an input
	OnConnectToServer();
	input_timer.Start(100);
}

/**
 *	This function continuosly loops searching for any data being input from the server
 */
void CLIENT::OnTimer(wxTimerEvent& event)
{
	// Constantly looks for an input from the server
	OnSocketInput();
}

/**
 *	This is the function that handles connecting to the server
 *	It will prompt the user to input the address and port of the server
 *	It will then create a socket to the server
 */
void CLIENT::OnConnectToServer()
{
	// Requests the user to input the server address and then the port number
	wxString hostname = wxGetTextFromUser(_("Enter the I.P. address of the Server:"));
	
	wxString PORT = wxGetTextFromUser(_("Enter the port number of the Server:"));
	
	// Creates the address of the server
	wxIPV4address addr;
	addr.Hostname(hostname);
	addr.Service(PORT);
	
	// Create the socket
	wxSocketClient* Socket = new wxSocketClient();
	
	// Set up the event handler and subscribe to most events
	
	Socket->SetEventHandler(*this, SOCKET_ID);
	Socket->SetNotify(wxSOCKET_CONNECTION_FLAG |
					  wxSOCKET_INPUT_FLAG |
					  wxSOCKET_LOST_FLAG);
	
	Socket->Notify(true);
	
	// Connect to the server
	Socket->Connect(addr, false);
}

/**
 *	This funcion initialises a socket for input
 *	It will then read data from the socket 
 *	It will then place the incoming text in the message_history text box
 *	When finished it will destroy the socket
 */
void CLIENT::OnSocketInput()
{
	// The socket that had will handle the input of data
	wxSocketBase* sock_in = new wxSocketBase();
	// Input data that will be forwarded to the GUI
	char DATAIN[255];
	for(int i=0;i<255;i++) DATAIN[i] = ' ';
	
	// Gets data from the server, sends it to the GUI then destroys the socket
	sock_in->Read(DATAIN, sizeof(DATAIN));
	message_history->AppendText(DATAIN);
	sock_in->Destroy();
}

/**
 *	Would ideally be called upon the pressing of the send button
 *	This function intialises a socket for output
 *	It will take the user input data from a text box
 *	It will then send the data to the server
 *	Upon completion it will then destroy the socket
 */
void CLIENT::OnSocketOutput()
{
	// The socket that will handle the output of data
	wxSocketBase* sock_out = new wxSocketBase();
	
	// Output data that will be taken from the GUI and sent to the server
	char DATAOUT[255];
	for(int i=0;i<255;i++) DATAOUT[i] = ' ';
	
	// Takes data from the GUI, sends it throught the socket and then destroys the socket
	DATAOUT = user_input_text_ctrl->GetLineText(0);
	sock_out->Write(DATAOUT, sizeof(DATAOUT));
	sock_out->Destroy();
}